﻿/*
 * copyright (c) 2009 hvbird
 *
 * This file is part of tdlib.
 *
 * tdlib is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * tdlib is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 */

using System;
using System.Collections;
using System.Text;
using System.Web;
using System.Text.RegularExpressions;

namespace TDLib.String
{
    public class HtmlUtils
    {
        /// <summary>
        /// 清除HTML标签
        /// </summary>
        /// <param name="HTML"></param>
        /// <returns></returns>
        public static string Clean(string HTML)
        {
            HTML = HtmlUtils.StripEntities(HTML, true);
            HTML = HtmlUtils.StripTags(HTML, true);
            HTML = HtmlUtils.StripPunctuation(HTML, false);

            return HTML;
        }

        public static string Clean(string HTML, bool nouse_now)
        {
            return Clean(HTML);
        }

        /// <summary>
        /// 格式化
        /// </summary>
        /// <param name="html"></param>
        /// <param name="retainSpace">保留空白</param>
        /// <returns></returns>
        public static string FormatText(string html, bool retainSpace)
        {
            string text2;
            string text1 = @"\s*<\s*[bB][rR]\s*/\s*>\s*";
            if (retainSpace)
            {
                text2 = " \n";
            }
            else
            {
                text2 = "";
            }
            return Regex.Replace(html, text1, text2);
        }

        /// <summary>
        /// 限制长度
        /// </summary>
        /// <param name="txt"></param>
        /// <param name="length"></param>
        /// <returns></returns>
        public static string Shorten(string txt, int length)
        {
            return Shorten(txt,length,"");
        }

        /// <summary>
        /// 限制长度
        /// </summary>
        /// <param name="txt"></param>
        /// <param name="length"></param>
        /// <param name="suffix"></param>
        /// <returns></returns>
        public static string Shorten(string txt, int length, string suffix)
        {
            if (txt.Length > length)
            {
                return txt.Substring(0, length) + suffix;
            }

            return txt;
        }


        /// <summary>
        /// 去除xml非法字符
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public static string EraseBadStr(string str)
        {
            return Regex.Replace(str, "[\\x00-\\x08\\x0b-\\x0c\\x0e-\\x1f]", "");
        }

        /// <summary>
        /// 去掉HTML内容
        /// </summary>
        /// <param name="HTML"></param>
        /// <param name="RetainSpace"></param>
        /// <returns></returns>
        public static string StripEntities(string HTML, bool RetainSpace)
        {
            string text1;
            if (RetainSpace)
            {
                text1 = " ";
            }
            else
            {
                text1 = "";
            }
            return Regex.Replace(HTML, "&[^;]*;", text1);
        }
        /// <summary>
        /// 去掉非文字内容
        /// </summary>
        /// <param name="HTML"></param>
        /// <param name="RetainSpace"></param>
        /// <returns></returns>
        public static string StripNonWord(string HTML, bool RetainSpace)
        {
            string text1;
            if (RetainSpace)
            {
                text1 = " ";
            }
            else
            {
                text1 = "";
            }
            if (HTML == null)
            {
                return HTML;
            }
            return Regex.Replace(HTML, @"\W*", text1);
        }
        /// <summary>
        /// 去掉标点
        /// </summary>
        /// <param name="HTML"></param>
        /// <param name="RetainSpace"></param>
        /// <returns></returns>
        public static string StripPunctuation(string HTML, bool RetainSpace)
        {
            string text3;
            string text1 = @"[~!#\$%\^&*\(\)-+=\{\[\}\]\|;:\x22'<,>\.\?\\\t\r\v\f\n]";
            Regex regex1 = new Regex(text1);
            Regex regex2 = new Regex(@"\s");
            string text2 = HTML;
            if (RetainSpace)
            {
                text3 = " ";
            }
            else
            {
                text3 = "";
            }
            while (regex2.IsMatch(text2))
            {
                text2 = regex2.Replace(text2, text3);
            }
            while (regex1.IsMatch(text2))
            {
                text2 = regex1.Replace(text2, text3);
            }
            return text2;
        }

        /// <summary>
        /// 去掉HTML标签
        /// </summary>
        /// <param name="HTML"></param>
        /// <param name="RetainSpace"></param>
        /// <returns></returns>
        public static string StripTags(string HTML, bool RetainSpace)
        {
            string text1;
            if (RetainSpace)
            {
                text1 = " ";
            }
            else
            {
                text1 = "";
            }
            return Regex.Replace(HTML, "<[^>]*>", text1);
        }

        /// <summary>
        /// 去掉空格
        /// </summary>
        /// <param name="HTML"></param>
        /// <param name="RetainSpace"></param>
        /// <returns></returns>
        public static string StripWhiteSpace(string HTML, bool RetainSpace)
        {
            string text1;
            if (RetainSpace)
            {
                text1 = " ";
            }
            else
            {
                text1 = "";
            }
            return Regex.Replace(HTML, @"\s+", text1);
        }
    }
}
